<?php

namespace App\model;

use App\City;
use App\ParkBaner;
use App\ParkFormat;
use App\ParkType;
use Illuminate\Database\Eloquent\Model;
use App\Website;

/**
 * App\model\Park
 *
 * @property int $id
 * @property string|null $logo 园区图片
 * @property string|null $area 园区面积
 * @property int|null $park_type_id 园区类型 关联表
 * @property string|null $title 园区名称
 * @property int|null $park_format_id 园区业态
 * @property int|null $city_id 区域id
 * @property string|null $is_hot 热度
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int|null $area_id 区域id
 * @property int|null $area1_id 下级区域id
 * @property-read \App\ParkFormat|null $parkFormat
 * @property-read \App\ParkType|null $parkType
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\model\Workshop[] $workshop
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereArea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereArea1Id($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereCityId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereIsHot($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereLogo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereParkFormatId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereParkTypeId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string|null $slider 园区图片
 * @property string|null $latitude 经度
 * @property string|null $longitude 纬度
 * @property string|null $des 园区概况
 * @property string|null $code
 * @property-read mixed $city
 * @property-read mixed $city_first
 * @property-read mixed $city_second
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\ParkBaner[] $parkBaner
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereDes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereLatitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereLongitude($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereSlider($value)
 * @property string|null $bottom 底部信息
 * @property int|null $park_area_id
 * @property string|null $tag
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereBottom($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereParkAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Park whereTag($value)
 */
class Park extends Model
{
    protected $table="park";
    protected $appends=['city','city_first','city_second'];
    public static function getSelectOptions()
    {
        $options = \DB::table('park')->select('id','title as text')->get();
        $selectOption = [];
        foreach ($options as $option){
            $selectOption[$option->id] = $option->text;
        }
        return $selectOption;
    }
    public function parkType(){

        return $this->belongsTo(ParkType::class,'park_type_id');
    }
    public function parkFormat(){
        return $this->belongsTo(ParkFormat::class,'park_format_id');
    }
    public function workshop(){
        return $this->hasMany(\App\model\Workshop::class,'park_id');
    }
    public function getSliderAttribute($value){
        $silder= explode(',',$value);
        $silder=array_filter($silder);
        $map=[];
        foreach ($silder as $k=> $v){
            if(preg_match("/(\.mp4|\.avi|\.wmv|\.mkv|\.rmvb|\.3gp)/",$v)){
                $map['video'][]=$v;
            }else{
                $webSite = Website::find(1);
                $url = config('admin.app_outurl1').'/'.$webSite->watermark.'?imageView2/2/w/360/h/250';
                $map['image'][]=$v.'?imageView2/2/w/960/h/720|watermark/1/image/'.\Qiniu\base64_urlSafeEncode($url).'/gravity/'.$webSite->watermark_position;
            }
            //$map[][]=config('filesystems.disks')[config('admin.upload.disk')]['url'].'/'.$v;
        }
        return $map;
    }
    public function getLogoAttribute($value)
    {
        $silder= explode(',',$this->attributes['slider']);
        $silder=array_filter($silder);
        $map=[];
        foreach ($silder as $k=> $v){
            if(!preg_match("/(\.mp4|\.avi|\.wmv|\.mkv|\.rmvb|\.3gp)/",$v)){
                return  $v;
            }
            //$map[][]=config('filesystems.disks')[config('admin.upload.disk')]['url'].'/'.$v;
        }

        return $value;
    }
    public function getCityAttribute(){
       $cityId= $this->attributes['city_id'];
        return  \Cache::remember('city_id_'.$cityId,4,function()use ($cityId){
           return  City::find($cityId)->title ?? '';
        }) ;
    }
    public function getCityFirstAttribute(){
        $cityId= $this->attributes['area_id'];
        return  \Cache::remember('area_id_'.$cityId,4,function()use ($cityId){
            return  City::find($cityId)->title ?? '';
        }) ;
    }
    public function getTagAttribute($value){
        return explode(',',$value);
    }
    public function getCitySecondAttribute(){
        $cityId= $this->attributes['area1_id'];
        return  \Cache::remember('area1_id_'.$cityId,4,function()use ($cityId){
            return  City::find($cityId)->title ?? '';
        });
    }
    public function parkBaner(){
        return $this->hasMany(ParkBaner::class,'park_id');
    }
}
